
/**
* @describe 基于社交游戏封装的游戏基础ws类 
* @author 游金宇(KM)
* @date 2024-09-12 11:47:51
*/
import { AllEventResponsePairs, AudienceOptions, ClientOption, MessageFns, SocialGameClient, SocialGameClientOption } from "sgc";
import { PitayaError } from "pitayaclient";
import { DEBUG } from "cc/env";
import { error, log } from "cc";
import { BinaryWriter } from "@bufbuild/protobuf/wire";


export type ExtendedClientOption = ClientOption & {
    /** 是否弹出请求错误信息 */
    showRequestErrInfo?: boolean;
};


export class WrapperSocialGameClient extends SocialGameClient {

    /**是否弹出请求错误信息 */
    protected showRequestErrInfo: boolean = false;

    /**日志黑名单 */
    protected logBlackList: string[] = []


    ignore: (route: string) => boolean = (item) => {
        return this.logBlackList.includes(item)
    }

    constructor(name: string, opts: Partial<SocialGameClientOption>, eventResponsePairs: Partial<AllEventResponsePairs>, clientOption?: ExtendedClientOption) {
        super(opts, eventResponsePairs, clientOption, name)
        this.showRequestErrInfo = clientOption?.showRequestErrInfo ?? false
    }

    destroy() {
        this.disconnect(true)
        globalThis._cat.socialGameClient.unregister(this)
    }

    override async GameRequest<U>(
        route: string,
        req: BinaryWriter,
        res: MessageFns<U>,
        {
            forwardReq = false,
            forwardResp = false
        }: AudienceOptions = {}
    ): Promise<U> {
        return new Promise((resolve, reject) => {
            super.GameRequest(route, req, res, {
                forwardReq,
                forwardResp
            }).then((res) => {
                if (DEBUG) {
                    log(
                        `%c ws服务端消息 %c [Response][${new Date()}] %c ${route} %c`,
                        'background:#ff00ff ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
                        'background:#3d7daa ; padding: 1px; color: #fff',
                        'background:#ff00ff ; padding: 1px; border-radius: 0 3px 3px 0;  color: #fff',
                        'background:transparent',
                        req
                    );
                }
                resolve(res)
            }, (err: PitayaError) => {
                error(err)
                this.showRequestErrInfo && err?.msg && globalThis._cat.gui.showToast({ title: err.msg })
                reject(err)
            })
        })
    }


    override  subscribe<T>(
        route: string, respType: MessageFns<T>, fn?: (data: T) => void
    ): this {
        super.subscribe(route, respType, (data) => {
            if (DEBUG && !this.ignore(route)) {
                log(
                    `%c ws服务端消息:[subscribe][${new Date()}] %c ${route} %c`,
                    'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
                    'background:#410083 ; padding: 1px; border-radius: 0 3px 3px 0;  color: #fff',
                    'background:transparent', data,
                );
            }
            globalThis._cat.event.emit(route, data)
            fn?.(data)
        })

        return this;
    }

}